<!--
 * @FilePath: /web-vue/src/views/SysUserDept/editForm.vue
 * @Description: 【用户部门关联表】编辑表单
-->
<template>
  <div>
    <!-- 弹出层-表单 -->
    <el-dialog
      class="edit-dialog"
      title="编辑【用户部门关联表】"
      :visible.sync="visible"
      :confirmLoading="confirmLoading"
      top="7vh"
    >
      <el-form
        :model="editForm"
        :rules="rules"
        ref="editForm"
        status-icon
        label-width="120px"
        style="width: 100%"
        class="layer-form-style"
      >
          <el-form-item label="ID：" prop="id" size="mini">
            <el-input v-model="editForm.id" ID readOnly></el-input>
          </el-form-item>
          <el-form-item label="用户ID：" prop="userId" size="mini">
            <el-input v-model="editForm.userId" placeholder="请输入用户ID" clearable></el-input>
          </el-form-item>
          <el-form-item label="部门ID：" prop="deptId" size="mini">
            <el-input v-model="editForm.deptId" placeholder="请输入部门ID" clearable></el-input>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          icon="el-icon-finished"
          @click="handleSubmit()"
          >保存
        </el-button>
        <el-button icon="el-icon-refresh-left" @click="handleCancel()"
          >取消
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const dateTimePickerOption = {
  shortcuts: [{
    text: '今天',
    onClick(picker) {
      picker.$emit('pick', new Date());
    }
  }, {
    text: '昨天',
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24);
      picker.$emit('pick', date);
    }
  }, {
    text: '一周前',
    onClick(picker) {
      const date = new Date();
      date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
      picker.$emit('pick', date);
    }
  }]
}

export default {
  data() {
    return {
      visible: false,
      confirmLoading: false,
      editForm: {
        id: null,
        userId: null,
        deptId: null,
      },
      // 表单校验规则
      rules: {
        id: [
          {
            required: true,
            message: "ID不能为空！",
            trigger: "blur",
          },
        ],
        userId: [
          {
            required: true,
            message: "用户ID不能为空！",
            trigger: "blur",
          },
        ],
        deptId: [
          {
            required: true,
            message: "部门ID不能为空！",
            trigger: "blur",
          },
        ],
      },
      // 日期选择快捷方式
      pickerOptions: dateTimePickerOption
    };
  },

  methods: {
    // 打开【用户部门关联表】表单编辑窗口
    openEditForm(isShow, item) {
      this.visible = isShow;
      // 【用户部门关联表】编辑信息填充（延迟执行，确保子组件初始化）
      setTimeout(() => {
        // 赋值
        this.editForm.id = item.id;
        this.editForm.userId = item.userId;
        this.editForm.deptId = item.deptId;
        // 清除表单校验结果
        this.$refs["editForm"].clearValidate();
      }, 1);
    },

    // 关闭【用户部门关联表】表单编辑窗口
    handleCancel() {
      this.visible = false;
      this.$refs["editForm"].resetFields();
    },

    // 提交
    handleSubmit() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            target: document.querySelector(".edit-dialog"),
            lock: true,
            text: "正在保存，请耐心等待！",
          });

          let vueIns = this;
          // 请求参数
          let param = Object.assign({}, this.editForm);
          // 发起请求
          this.$api
            .modifySysUserDept(param)
            .then((res) => {
              loading.close();
              let { status, message, data } = res.data; // 响应数据
              if (status == 200) {
                this.$emit("afterSubmit");
                this.handleCancel();
                // 提示信息
                this.$message({
                  type: "success",
                  message: `操作成功！已编辑【用户部门关联表】!${message}`,
                  duration: 1680,
                });
              } else if (status == 403) {
                this.$message({
                  message: `权限不足！${message}`,
                  type: "error",
                  center: true,
                  duration: 1680,
                });
                // this.$router.push("/login");
              } else {
                this.$alert(`操作失败！${message}`, "【用户部门关联表】编辑", {
                  type: "error",
                  confirmButtonText: "确定",
                  callback: (action) => {},
                });
              }
            })
            .catch(function(err) {
              loading.close();
              vueIns.visible = false;
              console.log(`【用户部门关联表】编辑接口发生错误：${err}`);
            });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// 表单布局样式
@import "@/assets/css/form-layout.scss";
</style>
